﻿@using Microsoft.AspNetCore.Mvc.Localization
@model TransactionListViewModel
@inject IViewLocalizer Localizer
@{
    ViewData["Title"] = @Localizer["Title"];
}
<br />
@{
    string timespan = (Model.Timespan == 2) ? "?t=2" : ((Model.Timespan == 3) ? "?t=3" : string.Empty);

    List<ConnectorStatusViewModel> connectorStatusViewModels = new List<ConnectorStatusViewModel>();

    // Copy CP-Names in dictionary for name resolution and
    Dictionary<string, string> chargePointNames = new Dictionary<string, string>();
    if (Model.ChargePoints != null)
    {
        foreach (ChargePoint cp in Model.ChargePoints)
        {
            chargePointNames.Add(cp.ChargePointId, cp.Name);
        }
    }

    // Count connectors for every charge point (=> naming scheme)
    Dictionary<string, int> dictConnectorCount = new Dictionary<string, int>();
    string currentConnectorName = string.Empty;
    foreach (ConnectorStatus cs in Model.ConnectorStatuses)
    {
        if (dictConnectorCount.ContainsKey(cs.ChargePointId))
        {
            // > 1 connector
            dictConnectorCount[cs.ChargePointId] = dictConnectorCount[cs.ChargePointId] + 1;
        }
        else
        {
            // first connector
            dictConnectorCount.Add(cs.ChargePointId, 1);
        }

        ConnectorStatusViewModel csvm = new ConnectorStatusViewModel();
        csvm.ChargePointId = cs.ChargePointId;
        csvm.ConnectorId = cs.ConnectorId;

        string connectorName = cs.ConnectorName;
        if (string.IsNullOrEmpty(connectorName))
        {
            // Default: use charge point name
            chargePointNames.TryGetValue(cs.ChargePointId, out connectorName);
            if (string.IsNullOrEmpty(connectorName))
            {
                // Fallback: use charge point ID
                connectorName = cs.ChargePointId;
            }
            connectorName = $"{connectorName}:{cs.ConnectorId}";
        }
        csvm.ConnectorName = connectorName;
        connectorStatusViewModels.Add(csvm);

        if (cs.ChargePointId == Model.CurrentChargePointId && cs.ConnectorId == Model.CurrentConnectorId)
        {
            currentConnectorName = connectorName;
        }
    }
}

<div class="container fill">
    <div class="row">
        <div class="col-md-auto align-self-center">
            @Localizer["ChargePointLabel"]
        </div>
        <div class="col-md-auto align-self-center">
            <div class="dropdown">
                <button class="btn btn-secondary dropdown-toggle" type="button" id="ddbChargePoint" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    @currentConnectorName
                </button>
                <div class="dropdown-menu" aria-labelledby="ddbChargePoint">
                    @foreach (ConnectorStatusViewModel csvm in connectorStatusViewModels)
                    {
                        <a class="dropdown-item" href="~/Home/Transactions/@Uri.EscapeDataString(csvm.ChargePointId)/@csvm.ConnectorId@timespan">@csvm.ConnectorName</a>
                    }
                </div>
            </div>
        </div>
        <div class="col-1">
        </div>
        <div class="col-md-auto align-self-center">
            @Localizer["TimeSpanLabel"]
        </div>
        <div class="col-md-auto align-self-center">
            <div class="dropdown">
                <button class="btn btn-secondary dropdown-toggle" type="button" id="ddbTimespan" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    @if (Model.Timespan == 2)
                    {
                        @Localizer["TimeSpan90"]
                    }
                    else if (Model.Timespan == 3)
                    {
                        @Localizer["TimeSpan365"]
                    }
                    else
                    {
                        @Localizer["TimeSpan30"]
                    }
                </button>
                <div class="dropdown-menu" aria-labelledby="ddbTimespan">
                    <a class="dropdown-item" href="~/Home/Transactions/@Uri.EscapeDataString(Model.CurrentChargePointId)/@Model.CurrentConnectorId">@Localizer["TimeSpan30"]</a>
                    <a class="dropdown-item" href="~/Home/Transactions/@Uri.EscapeDataString(Model.CurrentChargePointId)/@Model.CurrentConnectorId?t=2">@Localizer["TimeSpan90"]</a>
                    <a class="dropdown-item" href="~/Home/Transactions/@Uri.EscapeDataString(Model.CurrentChargePointId)/@Model.CurrentConnectorId?t=3">@Localizer["TimeSpan365"]</a>
                </div>
            </div>
        </div>
        <div class="col-3">
        </div>
        <div class="col-md-auto align-self-center">
            <a href="~/Home/Export/@Uri.EscapeDataString(Model.CurrentChargePointId)/@Model.CurrentConnectorId@timespan" data-toggle="tooltip" data-placement="top" title="@Localizer["DownloadCsv"]">
                <i class="fas fa-file-csv fa-2x"></i>
            </a>
            <a href="~/Home/ExportXlsx/@Uri.EscapeDataString(Model.CurrentChargePointId)/@Model.CurrentConnectorId@timespan" data-toggle="tooltip" data-placement="top" title="@Localizer["DownloadXlsx"]">
                <i class="fas fa-file-excel fa-2x"></i>
            </a>
        </div>
    </div>
</div>

<br />

@if (Model != null)
{
    <table id="dtTransactions" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th class="th-sm">@Localizer["Connector"]</th>
                <th class="th-sm">@Localizer["StartTime"]</th>
                <th class="th-sm">@Localizer["StartTag"]</th>
                <th class="th-sm">@Localizer["StartMeter"]</th>
                <th class="th-sm">@Localizer["StopTime"]</th>
                <th class="th-sm">@Localizer["StopTag"]</th>
                <th class="th-sm">@Localizer["StopMeter"]</th>
                <th class="th-sm">@Localizer["ChargeSum"]</th>
            </tr>
        </thead>
        <tbody>
            @if (Model.Transactions != null)
            {
                foreach (TransactionExtended t in Model.Transactions)
                {
                    <tr>
                        <td>@currentConnectorName</td>
                        <td>@string.Format("{0} {1}", t.StartTime.ToLocalTime().ToShortDateString(), t.StartTime.ToLocalTime().ToShortTimeString())</td>
                        <td>@(string.IsNullOrEmpty(t.StartTagName) ? t.StartTagId : t.StartTagName)</td>
                        <td>@string.Format("{0:0.0##}", t.MeterStart)</td>
                        <td>@((t.StopTime != null) ? string.Format("{0} {1}", t.StopTime.Value.ToLocalTime().ToShortDateString(), t.StopTime.Value.ToLocalTime().ToShortTimeString()) : string.Empty)</td>
                        <td>@(string.IsNullOrEmpty(t.StopTagName) ? t.StopTagId : t.StopTagName)</td>
                        <td>@((t.MeterStop != null) ? string.Format("{0:0.0##}", t.MeterStop) : string.Empty)</td>
                        <td>@((t.MeterStop != null) ? string.Format("{0:0.0##}", (t.MeterStop - t.MeterStart)) : string.Empty)</td>
                    </tr>
                }
            }
        </tbody>
    </table>
}
